package com.cow.controller;

import com.cow.entity.ShoppingCart;
import com.cow.service.ShoppingCartService;
import com.cow.util.general.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    //传参的几种情况：
    //1.跟在路径后面：url?name=zs&age=19
    //2.普通表单提交的参数，后端直接封装成对象，Content-Type:application/x-www-form-urlencoded
    //3.把参数绑定到请求体上，Content-Type: application/json，后端需要使用@RequestBody接受


    @PostMapping("/add")
    public CommonResult add(@RequestBody ShoppingCart shoppingCart){
        boolean b = shoppingCartService.addShoppingCart(shoppingCart);
        if (b) {
            return CommonResult.success("添加購物車成功");
        }else{
            return CommonResult.error("添加購物車失败");
        }
    }


    @GetMapping("/findAll")
    public CommonResult findAll(String account){
        List<Map<String, Object>> cartMap = shoppingCartService.queryCartWithAccount(account);
        return CommonResult.success("购物车查询成功",cartMap);
    }

    @DeleteMapping("/delete")
    public CommonResult delete(Integer cartId){
        boolean b = shoppingCartService.removeById(cartId);
        if (b){
            return CommonResult.success("删除成功");
        }
        return CommonResult.success("删除失败");
    }

}
